﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Specialized;

public partial class Admin_Gopy : System.Web.UI.Page
{
    DADataContext da = new DADataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var mcn = (from cn in da.Chucnangs where cn.Tenchucnang == "Quản lý góp ý" select cn.Machucnang).FirstOrDefault();
                var kt = (from a in da.TaikhoanChucnangs where (a.Taikhoan == Session["user"].ToString() && a.Machucnang == mcn) select a).Count();
                if (kt == 1)
                {

                    gvGY.DataSource = (from a in da.Gopies orderby a.NgayGY descending select a);
                    gvGY.DataBind();
                    ht.Visible = false;
                    if (gvGY.Rows.Count == 0)
                    {
                        hthi.Visible = false;
                    }
                    else
                    {
                        hthi.Visible = true;
                        tb.Text = "Không có góp ý nào !";
                    }
                    if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString["m"])))
                    {
                        ht.Visible = true;
                        int m = int.Parse(Request.QueryString["m"]);
                        var select = (from x in da.Gopies where x.MaGY == m select x).FirstOrDefault();
                        thongtin.Text = "Người gửi:" + select.Hoten + "<br /> Địa chỉ:" + select.Email + " <br />Ngày gửi:" + select.NgayGY.ToString() + "<br />";
                        noidung.Text = select.Noidung;


                    }
                }
                else
                    Response.Redirect("/Online/Admin/Default.aspx");
        }
    }
    protected void btnXoa_Click(object sender, EventArgs e)
    {
        StringCollection idConnllection = new StringCollection();
        string strids = string.Empty;

        for (int i = 0; i < gvGY.Rows.Count; i++)
        {
            CheckBox ck = (CheckBox)gvGY.Rows[i].Cells[0].FindControl("ckq1");
            if (ck != null)
            {
                if (ck.Checked == true)
                {
                    Label lb = (Label)gvGY.Rows[i].Cells[1].FindControl("Ma");
                    strids = lb.Text;
                    idConnllection.Add(strids);
                }
            }
        }
        if (idConnllection.Count > 0)
        {
            string iDS = ""; int a;
            foreach (string id in idConnllection)
            {
                iDS += id + ",";
            }
            string[] mang = iDS.Split(',');
            for (int i = 0; i < mang.Length - 1; i++)
            {
                a = int.Parse(mang[i]);
                var xoa = da.Gopies.SingleOrDefault(p => p.MaGY == a);
                if (xoa != null)
                {
                    da.Gopies.DeleteOnSubmit(xoa);
                    da.SubmitChanges();
                }
            }
            tb.Text = "Xóa thành công!";
            gvGY.DataSource = (from c in da.Gopies orderby c.NgayGY descending select c);
            gvGY.DataBind();
            ht.Visible = false;
        }
        else
            tb.Text = "Không bản ghi nào được chọn để xóa!";
    }
}
